package winOrDie.graphicsInterface.GameMenu;

import java.util.Vector;

import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.SWT;

public class Composite_Join_Game extends Composite {

	private Label label_Join_Game = null;

	private Composite composite_Peer_Direction = null;
	private Label label_Peer_Direction = null;
	private Text text_Peer_Direction = null;

	private Composite composite_Peer_Port = null;
	private Label label_Peer_Port = null;
	private Text text_Peer_Port = null;

	private Composite composite_Buttons = null;
	private Button button_Connect = null;
	private Button button_Cancel = null;

	private Vector<Composite_Join_Game_Listener> listeners = null;

	public synchronized void add_Composite_Join_Game_Listener(Composite_Join_Game_Listener listener)
	{
		listeners.add(listener);
	}

	public synchronized void remove_Composite_Join_Game_Listener(Composite_Join_Game_Listener listener)
	{
		listeners.remove(listener);
	}

	protected void fire_Composite_Join_Game_Event(Composite_Join_Game_Event event)
	{
		for (Composite_Join_Game_Listener listener : listeners) {
			listener.on_Composite_Join_Game_Changed(event);
		}
	}

	public Composite_Join_Game(Composite parent, int style) {
		super(parent, style);

		listeners = new Vector<Composite_Join_Game_Listener>();
		initialize();
	}

	private void initialize() {

		this.setSize(new Point(300, 216));

		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 1;
		setLayout(gridLayout);

		get_Label_JoinGame();
		get_Text_Peer_Direction();
		get_Text_Peer_Port();
		get_Composite_Buttons();
	}


	private Label get_Label_JoinGame()
	{
		if (label_Join_Game == null) {

			GridData gridData = new GridData();
			gridData.horizontalSpan = 1;

			label_Join_Game = new Label(this, SWT.NONE);
			label_Join_Game.setText("Unirse a juego");

			label_Join_Game.setLayoutData(gridData);
		}
		return label_Join_Game;
	}


	public Text get_Text_Peer_Direction()
	{
		if (text_Peer_Direction == null)
		{
			GridLayout gridLayout = new GridLayout();
			gridLayout.numColumns = 2;

			GridData gridData_Composite_Peer_Direction = new GridData();
			gridData_Composite_Peer_Direction.grabExcessHorizontalSpace = true;
			gridData_Composite_Peer_Direction.horizontalAlignment = SWT.FILL;

			composite_Peer_Direction = new Composite(this, SWT.NONE);
			composite_Peer_Direction.setLayout(gridLayout);
			composite_Peer_Direction.setLayoutData(gridData_Composite_Peer_Direction);

			label_Peer_Direction = new Label(composite_Peer_Direction, SWT.NONE);
			label_Peer_Direction.setText("IP:");

			GridData gridData_Text_Peer_Direction = new GridData();
			gridData_Text_Peer_Direction.grabExcessHorizontalSpace = true;
			gridData_Text_Peer_Direction.horizontalAlignment = SWT.FILL;
			text_Peer_Direction = new Text(composite_Peer_Direction, SWT.BORDER);
			text_Peer_Direction.setLayoutData(gridData_Text_Peer_Direction);

		}

		return text_Peer_Direction;
	}


	public Text get_Text_Peer_Port()
	{
		if (text_Peer_Port == null)
		{
			GridLayout gridLayout = new GridLayout();
			gridLayout.numColumns = 2;

			GridData gridData_Composite_Peer_Port = new GridData();
			gridData_Composite_Peer_Port.grabExcessHorizontalSpace = true;
			gridData_Composite_Peer_Port.horizontalAlignment = SWT.FILL;

			composite_Peer_Port = new Composite(this, SWT.NONE);
			composite_Peer_Port.setLayout(gridLayout);
			composite_Peer_Port.setLayoutData(gridData_Composite_Peer_Port);

			label_Peer_Port = new Label(composite_Peer_Port, SWT.NONE);
			label_Peer_Port.setText("Puerto");

			GridData gridData_text_Peer_Port = new GridData();
			gridData_text_Peer_Port.grabExcessHorizontalSpace = true;

			text_Peer_Port = new Text(composite_Peer_Port, SWT.BORDER);
			text_Peer_Port.setLayoutData(gridData_text_Peer_Port);

		}

		return text_Peer_Port;
	}



	private Composite get_Composite_Buttons()
	{
		if (composite_Buttons == null)
		{
			GridLayout gridLayout_Composite_Buttons = new GridLayout();
			gridLayout_Composite_Buttons.numColumns = 2;

			GridData gridData_Composite_Buttons = new GridData();
			gridData_Composite_Buttons.grabExcessHorizontalSpace = true;
			gridData_Composite_Buttons.horizontalAlignment = SWT.FILL;

			composite_Buttons = new Composite(this, SWT.NONE);
			composite_Buttons.setLayout(gridLayout_Composite_Buttons);
			composite_Buttons.setLayoutData(gridData_Composite_Buttons);

			/**
			 * Connect button
			 */
			GridData gridData_Button_Connect = new GridData();
			gridData_Button_Connect.grabExcessHorizontalSpace = true;
			gridData_Button_Connect.horizontalAlignment = SWT.CENTER;

			button_Connect = new Button(composite_Buttons, SWT.PUSH);
			button_Connect.setLayoutData(gridData_Button_Connect);
			button_Connect.setText("Conectar");

			final Composite_Join_Game compositeJoinGame = this;
			button_Connect.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					Composite_Join_Game_Event event = new Composite_Join_Game_Event(compositeJoinGame);
					event.setEventType(Composite_Join_Game_Event.Event_Types.CONNECT);
					fire_Composite_Join_Game_Event(event);
				}
			});


			/**
			 * Cancel button
			 */
			GridData gridData_Button_Cancel = new GridData();
			gridData_Button_Cancel.grabExcessHorizontalSpace = true;
			gridData_Button_Cancel.horizontalAlignment = SWT.CENTER;

			button_Cancel = new Button(composite_Buttons, SWT.PUSH);
			button_Cancel.setLayoutData(gridData_Button_Cancel);
			button_Cancel.setText("Cancelar");
			
			button_Cancel.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					Composite_Join_Game_Event event = new Composite_Join_Game_Event(compositeJoinGame);
					event.setEventType(Composite_Join_Game_Event.Event_Types.CANCEL);
					fire_Composite_Join_Game_Event(event);
				}
			});

		}

		return composite_Buttons;
	}










}  //  @jve:decl-index=0:visual-constraint="10,10"
